package 位运算;

public class 两整数之和8 {
    //https://leetcode.cn/problems/sum-of-two-integers/description/
    public int getSum1(int a, int b) {
        int sum = a ^ b;
        int leave = (a & b) << 1;
        while (leave != 0) { // 就是没有进位 没欠
//            int sum_tmp = sum;
//            sum ^= leave;
//            leave = (sum_tmp & leave) << 1;

//            int leave_tmp = (sum & leave) << 1;
//            sum ^= leave;
//            leave = leave_tmp;

            int tmp_leave = leave;
            leave = (sum & leave) << 1;
            sum ^= tmp_leave;
        }
        return sum;
    }
    public int getSum(int a, int b) {
        //这次无进位相加能否为结果
        while (b != 0) { // 进位为0 没欠
            int x = a ^ b; // 进行无进位相加
            int carry = (a & b) << 1; // 这次相加的进位
            a = x;
            b = carry;
        }
        return a;
    }
}
